//
//  TouristAddViewController.m
//  SXH
//
//  Created by Ryan on 16/3/24.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "TouristAddViewController.h"
#import "CommonService.h"
#import "LibController.h"
@interface TouristAddViewController ()

@end

#define K_nameNullWrong   @"姓名不能为空"
#define K_idWrong   @"请输入有效的身份证号"

@implementation TouristAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.model)
    {
        self.model = [[EditTouristModel alloc]init];
        self.model.identity = @"1";
    }
    else
    {
        self.touristName.text = self.model.title;
        self.touristNum.text = self.model.document_num;
        [self unselectAll];
        switch ([self.model.identity intValue]) {
            case 1:
                [self.selectAdult setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
                break;
            case 2:
                [self.selectOlder setImage:[UIImage imageNamed:K_noSelectedImageName] forState:UIControlStateNormal];
                break;
            case 3:
                [self.selectChild_NoBed setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
                break;
            case 4:
                [self.selectChild setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
                break;
            case 5:
                [self.selectBaby setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
                break;
                
                
            default:
                break;
        }
    }
}
//1 成人 2 老年人 3 小孩--不占铺 4 小孩--占铺 5 婴儿
- (IBAction)selectAdultBtn:(id)sender
{
    if (![self.model.identity isEqualToString:@"1"])
    {
        [self unselectAll];
        [self.selectAdult setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
        self.model.identity = @"1";
    }
    
}


- (IBAction)selectOlderBtn:(id)sender
{
    if (![self.model.identity isEqualToString:@"2"])
    {
        [self unselectAll];
        [self.selectOlder setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
        self.model.identity = @"2";
    }
}



- (IBAction)selectChild_NoBedBtn:(id)sender
{
    if (![self.model.identity isEqualToString:@"3"])
    {
        [self unselectAll];
        [self.selectChild_NoBed setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
        self.model.identity = @"3";
    }
}

- (IBAction)selectBabyBtn:(id)sender
{
    if (![self.model.identity isEqualToString:@"5"])
    {
        [self unselectAll];
        [self.selectBaby setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
        self.model.identity = @"5";
    }
}

- (IBAction)selectChildBtn:(id)sender
{
    if (![self.model.identity isEqualToString:@"4"])
    {
        [self unselectAll];
        [self.selectChild setImage:[UIImage imageNamed:K_selectedImageName] forState:UIControlStateNormal];
        self.model.identity = @"4";
    }
}

- (void)unselectAll
{
    [self.selectAdult setImage:[UIImage imageNamed:K_noSelectedImageName] forState:UIControlStateNormal];
    [self.selectChild setImage:[UIImage imageNamed:K_noSelectedImageName] forState:UIControlStateNormal];
    [self.selectOlder setImage:[UIImage imageNamed:K_noSelectedImageName] forState:UIControlStateNormal];
    [self.selectChild_NoBed setImage:[UIImage imageNamed:K_noSelectedImageName] forState:UIControlStateNormal];
    [self.selectBaby setImage:[UIImage imageNamed:K_noSelectedImageName] forState:UIControlStateNormal];

}

- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//title 人员姓名
//identity  人员身份
//document_type  1 身份证 2 护照 3 港澳通行证
//document_num   证件号码
- (IBAction)complete:(id)sender {
    CommonService *service = [[CommonService alloc]init];
    if (self.touristName.text.length <= 0)
    {
        ALERT_ERR_MSG(K_nameNullWrong);
        return;
    }
    if (![LibController verifyIDCardNumber:self.touristNum.text])
    {
        ALERT_ERR_MSG(K_idWrong);
        return;
    }
    NSDictionary *parm = nil;
    if (self.model.id)
    {
        parm = @{@"title":self.touristName.text,
                               @"identity":self.model.identity,
                               @"document_type":@"1",
                               @"document_num":self.touristNum.text,
                               @"id":self.model.id
                 };
    }
    else
    {
        parm = @{@"title":self.touristName.text,
                 @"identity":self.model.identity,
                 @"document_type":@"1",
                 @"document_num":self.touristNum.text,
                 @"insert_user_id":USERID
                 };
    }
    [service getNetWorkData:parm Path:TRAVEL_Edit Successed:^(id entity) {
        [self.navigationController popViewControllerAnimated:YES];
        [self.parentVC getTouristInfo];
    } Failed:^(NSInteger errorCode, NSString *message) {
        
    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
